FILE(GLOB EXAMPLES_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.java)

SET(CLASSPATH "${JBLAS}:${SHOGUN_JAR}:${CMAKE_CURRENT_BINARY_DIR}")
SET(JAVAOPTS "-Xmx1024m")
SET(JAVA_LIB_PATH "${JAVA_MODULAR_BUILD_DIR}")

FOREACH(EXAMPLE_SRC ${EXAMPLES_SRC})
	get_filename_component(EXAMPLE_NAME ${EXAMPLE_SRC} NAME_WE)

	ADD_CUSTOM_COMMAND(OUTPUT ${EXAMPLE_NAME}.class
				  COMMAND ${Java_JAVAC_EXECUTABLE} -cp ${CLASSPATH}
					-d ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLE_SRC}
				  DEPENDS java_modular)
	LIST(APPEND JAVA_EXAMPLES ${EXAMPLE_NAME}.class)

	add_test(NAME generated_java_modular-${EXAMPLE_NAME}
			WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
			COMMAND ${Java_JAVA_EXECUTABLE} ${JAVAOPTS}
				-cp ${CLASSPATH}
				-Djava.library.path=${JAVA_LIB_PATH} ${EXAMPLE_NAME})
ENDFOREACH()

add_custom_target(build_generated_java_examples ALL
				 DEPENDS ${JAVA_EXAMPLES}
				 COMMENT "Generated java examples")